package com.example.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private TextView nowPath;
    private ListView lst;
    private Myadapter adapter;
    private List<Map<String,Object>> values;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Globals.init(this);
        setContentView(R.layout.activity_main);
        nowPath = (TextView) findViewById(R.id.nowPath);
        lst = (ListView) findViewById(R.id.list);

        values = new ArrayList<>();
        File sd = Environment.getExternalStorageDirectory();
        Log.i("tag",sd.getAbsolutePath());
        load(sd);
       adapter = new Myadapter(values,this);
        lst.setAdapter(adapter);
        lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String, Object> map = values.get(position);
                File f = new File(map.get("fullPath").toString());
                boolean isDir = (boolean) map.get("isDir");
                if (isDir) {
                    values.clear();
                    if (!Environment.getExternalStorageDirectory().getAbsolutePath().equals(f.getAbsolutePath())) {
                        Map<String, Object> parentData = new HashMap<String, Object>();
                        parentData.put("exName", "tool");
                        parentData.put("fileName", "返回上一级");
                        parentData.put("isDir", true);
                        parentData.put("fullPath", f.getParent());
                        parentData.put("real", "TRUE");
                        values.add(parentData);
                    }
                    load(f);
                    Log.i("tag", values.toString());
                    adapter.notifyDataSetChanged();
                } else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("文件信息");
                    builder.setMessage("文件大小为：" + f.length() + "\r\n" + "文件名为：" + f.getName());
                    builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    builder.create().show();

                }
            }
        });
        lst.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(final AdapterView<?> parent, View view, final int position, long id) {
                final Map<String,Object> map = values.get(position);
                boolean isDir = (boolean) map.get("isDir");
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("提示信息");
                builder.setMessage("您确定要删除吗");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        File f = new File(map.get("fullPath").toString());
                        f.delete();
                        values.remove(position);
                        adapter.notifyDataSetChanged();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.create().show();
                return false;
            }
        });
    }
//遍历文件
    private void load(File path) {
        File[] fs = path.listFiles();
        nowPath.setText(path.getAbsolutePath());
        for(File f:fs){
           Map<String,Object> map = new HashMap<>();
           String fileName = f.getName();
           map.put("fileName",fileName);
            map.put("fullPath",f.getAbsolutePath());
            map.put("isDir", true);
           if(f.isDirectory()){
               map.put("exName", "file");
           }
           else{
               map.put("exName",fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase());
               map.put("isDir",false);
           }
           values.add(map);

       }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==event.KEYCODE_BACK){
            Map<String,Object> map = values.get(0);
            if("TRUE".equals(map.get("real"))){
                lst.performItemClick(lst.getChildAt(0), 0, lst.getChildAt(0).getId());
            }
            else{
                AlertDialog.Builder bu = new AlertDialog.Builder(MainActivity.this);
                bu.setTitle("提示信息");
                bu.setMessage("真的要离开吗？");
                bu.setPositiveButton("真的", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });
                bu.setNegativeButton("骗你的", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                bu.create().show();
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}
